package com.zzmm.socket.qq.client;

import com.zzmm.socket.qq.common.Message;
import com.zzmm.socket.qq.common.ConsoleUtil;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileInputStream;

import static com.zzmm.socket.qq.common.QQConstant.MessageType.*;

public class ClientService {

    private ClientSocketManager clientSocketManager;

    @SneakyThrows
    public boolean login() {
        // 获取用户输入的账号密码
        String username = ConsoleUtil.getString("请输入用户号：");
        String password = ConsoleUtil.getString("请输入密 码：");

        // 管理客户端与服务器的通信线程
        try {
            clientSocketManager = new ClientSocketManager(username, password);
        } catch (Exception e) {
            ConsoleUtil.printf("登录失败：%s\n", e.getMessage());
            return false;
        }

        return true;
    }

    /**
     * 获取用户名
     */
    public String getUserName() {
        return clientSocketManager.getUsername();
    }


    /**
     * 获取在线用户
     */
    @SneakyThrows
    public void listOnlineUser() {
        // 发送请求
        Message message = Message.builder()
                .messageType(LIST_ONLINE_USER)
                .build();
        clientSocketManager.writeMessage(message);
    }

    /**
     * 私聊
     */
    public void privateChat() {
        String receiver =  ConsoleUtil.getString("消息的接收人是：");
        String content = ConsoleUtil.getString("消息的内容是：");

        Message message = Message.builder()
                .content(content)
                .messageType(PRIVATE_CHAT)
                .receiver(receiver)
                .build();
        clientSocketManager.writeMessage(message);

    }

    /**
     * 退出
     */
    public void exit() {

        // 未登录
        if (clientSocketManager == null) {
            return;
        }

        // 这边不能直接调用writeMessage
        Message message = Message.builder()
                .messageType(LOGOUT)
                .build();
        clientSocketManager.writeMessage(message);

    }

    /**
     * 群聊
     * 将消息发给除自己以外的所有人
     */
    public void groupChat() {
        String content = ConsoleUtil.getString("请输入群发内容：");

        Message message = Message.builder()
                .messageType(GROUP_CHAT)
                .content(content)
                .build();
        clientSocketManager.writeMessage(message);
    }

    /**
     * 发送文件
     */
    @SuppressWarnings({"all"})
    @SneakyThrows
    public void sendFile() {
        String receiver = ConsoleUtil.getString("文件的接收人是：");
        String filePath = ConsoleUtil.getString("文件的地址是：");
        File file = new File(filePath);
        if (!file.isFile()) {
            ConsoleUtil.println("无效的文件地址：" + filePath);
            return;
        }

        // 读取文件
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        fileInputStream.read(data);
        fileInputStream.close();

        // 发送
        Message message = Message.builder()
                .content(data)
                .receiver(receiver)
                .messageType(FILE)
                .build();
        clientSocketManager.writeMessage(message);
    }
}
